import dayjs from "dayjs";
import * as ExcelJS from "exceljs/dist/exceljs.min.js";
import FileSaver from "file-saver";
import { ElNotification, ElMessage, ElMessageBox } from "element-plus";

export default function ({
  targetDate,
  version,
  tableHourlyHeader,
  tableHourlyData,
}) {
  const workbook = new ExcelJS.Workbook();

  const worksheet = workbook.addWorksheet("粘贴表", {
    views: [{ state: "frozen", xSplit: 3, ySplit: 1 }],
  });

  worksheet.columns = tableHourlyHeader.map((item) => ({
    header: item.label,
    key: item.prop,
    width: item.column == -1 ? 14 : 8,
  }));

  tableHourlyData.forEach((row) => {
    worksheet.addRow(row);
  });

  worksheet.properties.defaultRowHeight = 15;

  worksheet.eachRow(function (row, rowNumber) {
    row.eachCell(function (cell, colNumber) {
      cell.alignment = {
        vertical: "middle",
        horizontal: "center",
        wrapText: true,
      };
      cell.font = { size: 12 };
      if (rowNumber == 1) {
        cell.font.bold = true;
      }
      if (tableHourlyHeader[colNumber - 1].column == -1) {
        cell.font.bold = true;
      }
    });
  });

  console.log("targetDate", targetDate, dayjs(targetDate));
  let fileName =
    "终端管制室_" +
    dayjs(targetDate).format("YYYY年MM月DD日") +
    "_决策量表_粘贴版_更新于_" +
    dayjs(version).format("DD日HH时mm分");
  workbook.xlsx.writeBuffer().then((buffer) => {
    const fileData = new Blob([buffer], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8",
    });
    FileSaver.saveAs(fileData, fileName + ".xlsx");
    // resolve(fileName);
  });
}
